import Vue from 'vue';
import VueRouter from 'vue-router';
import { Notification } from "element-ui";

/**
 * 引入路由模块
 */
import settingsRouter from "@/router/modules/settings";
import studentMessageRouter from "@/router/modules/student-message";
import downlineRouter from "@/router/modules/downline";
import bidRouter from "@/router/modules/bid";
import taskRouter from "@/router/modules/task";
import studentRouter from "@/router/modules/student";

Vue.use(VueRouter);

// const originalPush = Router.prototype.push;

// Router.prototype.push = function push(location) {
//     return originalPush.call(this, location).catch(err => err);
// };

const router = new VueRouter({
    mode: 'history',
    routes: [
        {
            path: '/login',
            name: 'Login',
            component: resolve => require(['@/views/account/login'], resolve)
        },
        {
            path: '/',
            component: resolve => require(['@/views/index/index'], resolve),
            children: [
                {
                    path: '',
                    component: resolve => require(['@/views/index/main'], resolve)
                },
                // 无权限页面
                {
                    path: '/no-auth',
                    name: 'NoAuth',
                    component: resolve => require(['@/views/others/no-auth'], resolve)
                },
                /** 设置模块模块路由 **/
                settingsRouter,
                /** 学生信息管理模块路由 **/
                studentMessageRouter,
                /** 校园工作室管理模块路由 **/
                downlineRouter,
                /** 广告系统模块路由 **/
                bidRouter,
                /** 任务系统模块路由 **/
                taskRouter,
                /** 校园代理模块路由 **/
                studentRouter,
            ]
        }
    ]
});

// 处理更新时路由跳转报错
router.onError((error) => {
    const pattern = /Loading chunk (\d)+ failed/g;
    const isChunkLoadFailed = error.message.match(pattern);

    if (isChunkLoadFailed) {
        Notification.info({
            title: '提示',
            message: '系统存在更新，正在刷新本地缓存...',
            duration: 1500
        });
        window.location.reload();
    }
});

export default router;
